package com.project.easy.common.util.util;

import com.project.easy.common.util.exception.file.CutImageException;
import com.project.easy.common.util.exception.file.FileNotExistException;
import com.project.easy.common.util.exception.file.FileNotReadException;
import com.project.easy.common.util.exception.print.PrintException;
import com.project.easy.common.util.io.CloseResouce;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/9/5 21:08
 * @description: 图片处理工具类
 */
public final class ImageUtil {
    /**
     * 私有构造方法
     */
    private ImageUtil() {
    }
    /**
     * @param originalImg 图片地址
     * @param out 输出
     * @param size 大小
     * @return true 成功
     * @throws FileNotExistException 文件不存在
     * @throws FileNotReadException 文件不可读
     */
    public static   Boolean cut(String originalImg, String out, int size) throws FileNotExistException, FileNotReadException {
        // 读入大图
        File file = new File(originalImg);
        if (!file.exists()) {
            throw  new FileNotExistException(String.format("%s文件不存在", originalImg));
        }
        if (!file.canRead()) {
            throw  new FileNotExistException(String.format("%s文件不可读取", originalImg));
        }
        File outs = new File(out);
        if (!outs.exists()) {
            outs.mkdirs();
        }
        int i1 = originalImg.lastIndexOf(".");
        String pre = originalImg.substring(i1, originalImg.length());
        FileInputStream fis = null;
        BufferedImage  image = null;
        try {
            fis = new FileInputStream(file);
            image = ImageIO.read(fis);
            int[] ints = calNum(Long.valueOf(file.length()).intValue(), 1024 * size);
            // 计算每个小图的宽度和高度
            int rows = ints.length;
            int chunkWidth = image.getWidth();
            int chunkHeight = image.getHeight() / rows;
            //大图中的一部分
            BufferedImage[] imgs = new BufferedImage[rows];
            for (int y = 0; y < rows; y++) {
                imgs[y] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
                //写入图像内容
                Graphics2D gr = imgs[y].createGraphics();
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, 0, chunkHeight * y, chunkWidth, chunkHeight * y + chunkHeight, null);
                gr.dispose();
            }
            // 输出小图
            for (int i = 0; i < imgs.length; i++) {
                ImageIO.write(imgs[i], "jpg", new File(out + i + pre));
                imgs[i] = null;
            }
        } catch (Exception e) {
            PrintException.print(e);
            throw new CutImageException("图片分割失败");
        } finally {
            CloseResouce.closes(fis);
        }
        return Boolean.TRUE;
    }

    /**
     * @param totalSize 总大小
     * @param size 大小
     * @return 返回高度数组
     */
    private static  int[] calNum(Integer totalSize, Integer size) {
        if (null == totalSize || totalSize == 0) {
            return null;
        }
        if (null == size || size == 0) {
            return null;
        }
        int temp = totalSize % size;
        int len = totalSize / size;
        if (temp != 0) {
            len = len + 1;
        }
        int[]  result = new int[len];
        for (int index = 0; index < len; index++) {
            if ((index == (len - 1))) {
                result[index] = totalSize - size * index;
            } else {
                result[index] = size;
            }

        }
        return result;
    }

}
